;(function($) {
    var _localStorage = null;
    var _sessionStorage = null;
    
    if (typeof (window.JSON) !== 'undefined' && typeof (Storage) !== "undefined") {
        _localStorage = window.localStorage;
        _sessionStorage = window.sessionStorage;
    }

    $.browserStorage = function () {
        return this.impl.isAvailable();
    };
    
    $.browserStorage.setItem = function (key, value, pesistent) {
        return this.impl.setItem(key, value, pesistent);
    };
    
    $.browserStorage.getItem = function (key, pesistent) {
        return this.impl.getItem(key, pesistent);
    };
    
    $.browserStorage.removeItem = function (key, pesistent) {
        return this.impl.removeItem(key, pesistent);
    };
    
    $.browserStorage.getItems = function (pesistent) {
        return this.impl.getItems(pesistent);
    };
	
	$.browserStorage.impl = {
		
	    isAvailable: function() {
            return _localStorage != null &&
            _sessionStorage != null;
        },

	    setItem: function (key, value, pesistent) {
	        if (!this.isAvailable)
	            return false;
	        pesistent = typeof (pesistent) !== 'undefined'
	            ? pesistent
	            : false;
	        if(pesistent)
	            _localStorage.setItem(key, this.serializeItem(value));
	        else
	            _sessionStorage.setItem(key, this.serializeItem(value));
		    return true;
		},
		
	    getItem: function (key, pesistent) {
	        if (!this.isAvailable)
	            return null;
	        pesistent = typeof (pesistent) !== 'undefined'
	            ? pesistent
	            : false;
	        return this.deserializeItem(pesistent ?
	            _localStorage.getItem(key) :
	            _sessionStorage.getItem(key));
	    },
		
	    removeItem: function (key, pesistent) {
	        if (!this.isAvailable)
	            return;
	        pesistent = typeof (pesistent) !== 'undefined'
	            ? pesistent
	            : false;
	        if (pesistent)
	            _localStorage.removeItem(key);
	        else 
	            _sessionStorage.removeItem(key);
		},
		
	    getItems: function (pesistent) {
			var items = new Array();
			if (!this.isAvailable)
			    return items;
			pesistent = typeof (pesistent) !== 'undefined'
			    ? pesistent
			    : false;
	        if (pesistent) {
	            for (var i1 in _localStorage) {
	                if (!i1.length)
	                    continue;
	                items.push({
	                    key: i1,
	                    value: this.deserializeItem(_localStorage.getItem(i1))
	                });
	            }
	        } else {
	            for (var i2 in _sessionStorage) {
	                if (!i2.length)
	                    continue;
	                items.push({
	                    key: i2,
	                    value: this.deserializeItem(_sessionStorage.getItem(i2))
	                });
	            }
	        }
	        return items;
		},
		
		serializeItem: function (value) {
		    return JSON.stringify(value);
		},
		
		deserializeItem: function (value) {
		    var ret;
		    try {
		        ret = JSON.parse(value);
		    } catch(e) {
		        return null;
		    }
		    if (ret == 'true')
		        ret = true;
		    if (ret == 'false')
		        ret = false;
		    if (parseFloat(ret) == ret && typeof(ret) != "object")
		        ret = parseFloat(ret);
		    return ret;
		}
	};
})(jQuery);